﻿// #define VERBOSE_LOGGING

using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Threading;
using Neoseeker.NAMFox.Tests.Browsers;
using NUnit.Framework;
using Selenium;

namespace Neoseeker.NAMFox.Tests
{
    [SetUpFixture]
    public class Controller
    {
        private Process seleniumProcess;
        private NAMFoxSelenium selenium;

        [SetUp]
        public void Setup()
        {
            var seleniumDirectory = new DirectoryInfo(@"..\..\..\build\selenium-remote-control-1.0.1");

            var targetBrowserAttribute = Assembly.GetExecutingAssembly()
                .GetCustomAttributes(typeof(TargetBrowserAttribute), false)
                .Cast<TargetBrowserAttribute>()
                .FirstOrDefault();

            var browserConfiguration = targetBrowserAttribute == null ?
                new FirefoxConfiguration(new Version("3.6")) :
                targetBrowserAttribute.LoadBrowserConfiguration();

            // Copy our user extensions
            File.Copy(browserConfiguration.SeleniumExtensionsPath, Path.Combine(seleniumDirectory.FullName, "user-extensions.js"), true);

            // Start the server
#if VERBOSE_LOGGING
            string commandLine = "-jar selenium-server.jar -userExtensions user-extensions.js -log selenium.log";
#else
            string commandLine = "-jar selenium-server.jar -userExtensions user-extensions.js";
#endif

            ProcessStartInfo psi = new ProcessStartInfo("java.exe", commandLine);

            psi.WorkingDirectory = seleniumDirectory.FullName;
            seleniumProcess = new Process();
            seleniumProcess.StartInfo = psi;
            seleniumProcess.Start();

            var commandProcessor = new HttpCommandProcessor("localhost", 4444, browserConfiguration.GetStartCommand(), Url.RootUri.Scheme + "://" + Url.RootUri.Host);
            selenium = new NAMFoxSelenium(commandProcessor);
            selenium.TargetBrowser = targetBrowserAttribute.TargetBrowser;

            int retryCount = 0;
            bool initialized = false;

            while (retryCount < 5)
            {
                try
                {
                    selenium.Start();
                    initialized = true;
                    break;
                }
                catch (WebException)
                {
                    Console.WriteLine("Waiting five seconds for Selenium server to start.");
                    Thread.Sleep(5000);
                    ++retryCount;
                }
            }

            if (!initialized)
            {
                throw new TimeoutException("Timed out waiting for Selenium server to start.");
            }

            selenium.SetTimeout("45000");
            selenium.WindowMaximize();
            selenium.Initialize();
            TestBase.Selenium = selenium;
        }

        [TearDown]
        public void TearDown()
        {
            selenium.Stop();
            seleniumProcess.Kill();
        }
    }
}
